package com.ymttest.database.mongo;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class NoteMongoWapper{
 
	MongoDBDriver mdd;
	
	public NoteMongoWapper() throws Exception {
		String[] addresses = EnvSetup.getSystemUrl("set3_mongoip").split(";");
		int port = Integer.valueOf(EnvSetup.getSystemUrl("set3_mongoport"));
		List<Map<String, String>> addresslist = new ArrayList<Map<String, String>>();
		for (String address : addresses) {
			HashMap m = new HashMap();
			m.put("address", address);
			m.put("port", port);
			addresslist.add(m);

		}
		MongoDBDriver.setSystemMongoPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		mdd = new MongoDBDriver(addresslist, "YmatouSocial");//datebase
		mdd.setPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		//mdd.auth("authuser", "123456");
		mdd.selectCollection("Ymt_Social_OnLine");//数据库表名
	}
	
	/**
	 * 用NoteId查询NoteMongoBean 这个数据是用来保存买家笔记的
	 * 
	 * @param NoteId
	 * @return AccessToken
	 * @throws Exception
	 */
	public Map getNoteInfoByNoteId(int NoteId) throws Exception {
		Map re = null;
		Map scmb = null;
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"NoteId\":" + NoteId + "}", 1);
		try {
			if (list.size() > 0) {
				scmb = list.get(0);
				return scmb;
			}
		} catch (Exception e) {
			Logger.debug("err data NoteId:" + NoteId);
			e.printStackTrace();
		}
		return null;
	}
	
	/***
	 * 返回指定卖家下的所有通过审核的笔记count数
	 * @param SellerId 卖家id
	 * 
	 * @return passCounts 笔记总数
	 * */
	public int getPassCount(int SellerId) throws Exception{
		int passCounts = 0;
		
			passCounts = mdd.selectCount("{\"NewNoteInfo.SellerId\":"+ SellerId +",\"Action\":0,\"Status\":0,\"OldNoteInfo\":{$ne:null}}");
		
		return passCounts;
	}
	
	/**
	 * 返回指定卖家下的所有已审核、待审核、审核未通过的任意10条笔记
	 * @param SellerId 卖家id
	 * @param status 0：审核通过，1：待审核，2：审核未通过
	 * @return 
	 * */
	public List<Map> selectPart10(int SellerId,int status) throws Exception{
		ArrayList<Map> notInfo = null;
		if(status==0){
			notInfo = mdd.selectPartLimitToMap("{\"NewNoteInfo.SellerId\":"+ SellerId +",\"Action\":0,\"Status\":0,\"OldNoteInfo\":{$ne:null}}","{\"NoteId\":1,\"_id\":0}",10);
		}else{
			notInfo = mdd.selectPartLimitToMap("{\"NewNoteInfo.SellerId\":"+ SellerId +",\"Status\":"+status+",\"OldNoteInfo\":null}","{\"NoteId\":1,\"_id\":0}",10);
		}
		
		
		return notInfo;
		
	}
	
	/**
	 * 返回卖家主页里某买家的所有已审核、待审核、审核未通过的任意10条笔记
	 * @param SellerId 卖家id
	 * @param userId 买家id
	 * @return
	 * 
	 * */
	public List<Map> selectLoadPart10(int SellerId,int status,int userId) throws Exception{
		ArrayList<Map> notInfo = null;
		notInfo = mdd.selectPartLimitToMap("{\"NewNoteInfo.SellerId\":"+SellerId+",\"Status\":"+status+",\"OldNoteInfo\":null,\"Action\":0,\"UserId\":"+userId+"}","{\"NoteId\":1,\"_id\":0}",10);
		return notInfo;
		
	}
	
	/**
	 * 返回关键字匹配出的所有笔记数
	 * @param keyword 搜索关键字
	 * @return int 总数
	 * */
	public int selectCount(String keyword) throws Exception{
		int noteCount=0;
		//noteCount=mdd.selectCount("{\"NewNoteInfo.Content\":"+keyword+",\"Status\":0}");
		noteCount = mdd.selectCount("{$or:[{\"NewNoteInfo.Content\":{$regex:\""+keyword+"\"}},{\"NewNoteInfo.TagImage.TagInfo.Tages.TagVal\":{$regex:\""+keyword+"\"}}],\"Status\":0}");
		
		return noteCount;
		
	}
	
	/**
	 * 返回笔记匹配搜索结果
	 * @param keywords
	 * 
	 * */
	public List<Map> selectKeywordsNote(String keyword,int limit) throws Exception{
		List<Map> noteInfo=null;
		noteInfo = mdd.selectPartLimitToMap("{$or:[{\"NewNoteInfo.Content\":{$regex:\""+keyword+"\"}},{\"NewNoteInfo.TagImage.TagInfo.Tages.TagVal\":{$regex:\""+keyword+"\"}}],\"Status\":0}","{\"_id\":0,\"NoteId\":1}",limit);
	    return noteInfo;
	}
	
	/**
	 * 返回最热笔记 按照collectionnum倒序取10条
	 * */
	public List<Map> selecthotNotePart10() throws Exception{
		ArrayList<Map> hotNoteList = null;
		hotNoteList =  mdd.selectPartLimitToMap("{\"Status\":0,\"OldNoteInfo\":{$ne:null}}","{\"NoteId\":1,\"CollectionNum\":1,\"_id\":0}",10,"{\"CollectionNum\":-1}");
		
		return null;
		
	}
	
	
	/**拔草感言
	 * 获取一周内小编推荐的关联订单的买家笔记
	 * */
	public ArrayList getRecommendNotes(String start,String end) throws Exception{
		ArrayList notInfo = mdd.selectPart("{\"Status\":{$ne:2},\"OldNoteInfo\":{$ne:null},\"IsRecommend\":true,\"NoteSource\":0,\"NewNoteInfo.OrderId\":{$ne:null},\"NewNoteInfo.OrderId\":{\"$gt\":0},'AddTime':{\"$gte\":{\"$date\":'"+start+"'},\"$lte\":{\"$date\":'"+end+"'}}}");
		
		return notInfo;
		
	}
	
	/**
	 * 按sproductid获取一段时间内推荐的关联订单的已审核笔记
	 * */
	public ArrayList getOrderNotesBySproductid(String start,String end,String sproductid) throws Exception{
		
		ArrayList notInfo = mdd.selectPart("{\"Status\":0,\"OldNoteInfo\":{$ne:null},\"NewNoteInfo.OrderId\":{$ne:null},\"NewNoteInfo.OrderId\":{\"$gt\":0},\"NewNoteInfo.ProductIds\":\""+sproductid+"\",'AddTime':{\"$gte\":{\"$date\":'"+start+"'},\"$lte\":{\"$date\":'"+end+"'}}}");
		
		return notInfo;
		
	}
	
	/**
	 * 检查一条笔记是否关联订单且通过审核
	 * 
	 * */
	public ArrayList getNoteBynoteId(int noteid) throws Exception{
		ArrayList noteInfo = mdd.selectPart("{\"Status\":0,\"OldNoteInfo\":{$ne:null},\"NewNoteInfo.OrderId\":{$ne:null},\"NewNoteInfo.OrderId\":{\"$gt\":0},\"NoteId\":"+noteid+"}");
		return noteInfo;
	}
	
	
	
	/**
	 * 修改笔记addtime时间为当前时间前一天
	 * */
	public void updateDataAddTime(String sproductid,String addtime) throws Exception{
		int status=0;
		
		mdd.update("{\"NewNoteInfo.ProductIds\":\""+sproductid+"\"}", "{\"AddTime\":{$date:\""+addtime+"\"},\"Status\":\""+status+"\"}");
	}
	
	/**
	 * 修改笔记status为未通过审核
	 * */
	public void updateDataStatus(int status,int noteid,String sproductid) throws Exception{
		mdd.update("{\"NewNoteInfo.ProductIds\":\""+sproductid+"\",\"NoteId\":\""+noteid+"\"}", "{\"Status\":\""+status+"\"}");
	}
	
	
	/**TODO 缺省sql
	 * 买手生活故事
	 * 获取所有有效的买手笔记
	 * */
	public ArrayList getsellerNotes() throws Exception{
		ArrayList notInfo = mdd.selectPart("");
		
		return notInfo;
	}
	
	
	
	
	public static void main(String args[]) throws Exception {
		// TODO Auto-generated method stub
		try {
			NoteMongoWapper notes = new NoteMongoWapper();
			//List<Map> noteInfo = notes.selectKeywordsNote("牛奶",5);
			/*Date date = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			String end = sdf.format(date);
			
			 Calendar c = Calendar.getInstance();  
		        c.add(Calendar.DATE, - 7);  
		        Date monday = c.getTime();
		        String start = sdf.format(monday);*/
			
			String end = YMTDateUtil.getDateForMongoFormat(0, 0, 0);
			String start = YMTDateUtil.getDateForMongoFormat(-31, 0, 0);
			String sproductid = "cddc434a-9767-4fd8-b55e-676383c84a10";

		 ArrayList n1 = notes.getNoteBynoteId(10014);
		
			
			// String findJson = "{\"Status\":0,\"OldNoteInfo\":{$ne:null},\"NewNoteInfo.OrderId\":{$ne:null},\"NewNoteInfo.OrderId\":{\"$gt\":0},\"NewNoteInfo.ProductIds\":\""+sproductid+"\",'AddTime':{\"$gte\":{\"$date\":'"+start+"'},\"$lte\":{\"$date\":'"+end+"'}}}";
			 System.out.println(n1.isEmpty());  
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void close() throws Throwable {
		mdd.close();
	}

}
